package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 *   PaymentService  保存 payment_info
 *   AlipayService   支付宝支付
 *   WeChatService   微信支付
 *
 */
@Service
public class AlipayServiceImpl implements AlipayService {


    @Autowired
    private PaymentService paymentService;
    @Autowired
    private OrderFeignClient orderFeignClient;
    @Autowired
    private AlipayClient alipayClient;

    //进入支付宝页面  先保存支付信息表
    @Override
    public String createAlipay(Long orderId) {
        //1:查询订单
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
        //2:保存支付信息
        paymentService.savePaymentInfo(orderInfo, PaymentType.ALIPAY.name());
        //设置请求参数
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //设置异步回调通知路径
        request.setNotifyUrl(AlipayConfig.notify_payment_url);
        //同步回调路径
        request.setReturnUrl(AlipayConfig.return_payment_url);
        //入参
        Map<String,Object> map = new HashMap<>();
        map.put("out_trade_no",orderInfo.getOutTradeNo());
        map.put("product_code","FAST_INSTANT_TRADE_PAY");
        //map.put("total_amount",orderInfo.getTotalAmount());
        map.put("total_amount","0.01");
        map.put("subject",orderInfo.getTradeBody());
        request.setBizContent(JSON.toJSONString(map));
        try {
            return alipayClient.pageExecute(request).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String refund(String out_trade_no) {

        PaymentInfo paymentInfo = paymentService.getPaymentInfo(out_trade_no, PaymentType.ALIPAY.name());

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        Map map = new HashMap();
        map.put("out_trade_no",paymentInfo.getOutTradeNo());
        map.put("refund_amount","0.01");
        //map.put("refund_amount",paymentInfo.getTotalAmount());
        map.put("refund_reason","正常退款");

        request.setBizContent(JSON.toJSONString(map));
        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            //更新支付信息
            paymentInfo.setPaymentStatus(PaymentStatus.ClOSED.name());
            paymentService.update(paymentInfo);
        }
        return response.getBody();

    }

    //关闭支付宝
    @Override
    public void closePay(PaymentInfo paymentInfo) {
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();

        Map map = new HashMap();
        //map.put("trade_no","");
        map.put("out_trade_no",paymentInfo.getOutTradeNo());
        map.put("operator_id","YX01");

        request.setBizContent(JSON.toJSONString(map));
        try {
            AlipayTradeCloseResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                System.out.println("关闭支付成功");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("关闭支付异常：可无需关闭");
        }
    }
}
